package com.feilong.edgeAI.remind.room.ui;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


import androidx.lifecycle.Observer;

import com.feilong.edgeAI.R;
import com.feilong.edgeAI.remind.alarm.AlarmClockHelper;
import com.feilong.edgeAI.remind.alarm.AlarmReceiver;
import com.feilong.edgeAI.remind.room.bean.RemindBean;
import com.feilong.edgeAI.remind.room.db.RemindDatabase;

import java.util.Date;


public class EditDialog extends BaseDialog implements View.OnClickListener , Observer {

    private EditText mDate, mMessage;
    private RemindBean remindBean;
    private OnRefreshDataListener mListener;

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_edit_info;
    }

    @Override
    protected void initView(View view) {
        mDate = view.findViewById(R.id.update_name_edit);
        mMessage = view.findViewById(R.id.update_phone_edit);
        view.findViewById(R.id.update_button).setOnClickListener(this);
        view.findViewById(R.id.delete_button).setOnClickListener(this);
    }

    @Override
    protected void loadData(Bundle bundle) {

        if (bundle != null) {
            remindBean = bundle.getParcelable("RemindBean");
            mDate.setText(remindBean.getTime());
            mMessage.setText(remindBean.getMessage());
        }
    }

    @Override
    public void onClick(View v) {
        String mName = mDate.getText().toString();
        String mPhone = mMessage.getText().toString();
        switch (v.getId()) {
            case R.id.update_button:
                if (mName.isEmpty()) {
                    Toast.makeText(getActivity(), "日期不能为空", Toast.LENGTH_SHORT).show();
                } else if (mPhone.isEmpty()) {
                    Toast.makeText(getActivity(), "内容不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    updatePhone(mName, mPhone);
                }
                break;
            case R.id.delete_button:
                deletePhone();
                break;
        }
    }

    private void updatePhone(String name, String phone) {
//        mPhoneBean.setName(name);
//        mPhoneBean.setPhone(phone);
//        mPhoneBean.setDate(new Date());
//        RemindDatabase.getDefault(getActivity().getApplicationContext()).getRemindDao().update(null);
//        dismiss();
//        if (mListener != null) {
//            mListener.onRefresh();
//        }
    }

    private void deletePhone() {
        RemindDatabase.getDefault(getActivity().getApplicationContext()).getRemindDao().delete(remindBean);

        AlarmClockHelper.cancelAlarmClock(getActivity().getApplicationContext(),remindBean.getTimeStamp());
        if (mListener != null) {
            mListener.onRefresh();
        }
        dismiss();

    }

    public void setOnRefreshDataListener(OnRefreshDataListener listener) {
        this.mListener = listener;
    }

    @Override
    public void onChanged(Object o) {

    }

    public interface OnRefreshDataListener {
        void onRefresh();
    }
}
